package cn.sccl.dirms.security.controller;

import cn.sccl.dirms.security.model.RoleAndAuthority;
import cn.sccl.dirms.security.service.RoleAndAuthorityManager;
import com.fasterxml.jackson.annotation.JsonView;
import cn.sccl.common.model.PageResult;
import cn.sccl.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 杨浩宇 on 2016-02-24.
 */
@RestController
@RequestMapping(value = "/dirms/security/roleAndAuthority")
public class RoleAndAuthorityController extends BaseController {

    @Autowired
    private RoleAndAuthorityManager roleAndAuthorityManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<RoleAndAuthority> listAllRoleAndAuthority() {
        return roleAndAuthorityManager.listAll();
    }

    @RequestMapping(value = "/data/{roleId}", method = RequestMethod.GET)
    public List<RoleAndAuthority> findRoleAndAuthority(@PathVariable("roleId") String id) {
        return roleAndAuthorityManager.findRoleAndAuthId(id);
    }


    /**
     * 分页查询
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<RoleAndAuthority> pageQueryRoleAndAuthority(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return roleAndAuthorityManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 保存数据
     *
     * @param roleAndAuthority
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public RoleAndAuthority saveRoleAndAuthority(@RequestBody RoleAndAuthority roleAndAuthority) {
        roleAndAuthorityManager.save(roleAndAuthority);
        return roleAndAuthority;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST,RequestMethod.GET})
    @JsonView(Object.class)
    public boolean deleteRoleAndAuthority(@PathVariable("id") String id) {
        roleAndAuthorityManager.delete(id);
        return true ;
    }
}
